<?php

namespace app\agentapi\controller;

use app\admin\model\BaseSetting as SettingModel;
use app\admin\model\article\UploadFile;
use OSS\OssClient;
use think\Session;
use think\Image;
use think\Db;
use think\Request;

/**
 * 试岗测试接口调用
 */
class Index extends Controller{
    private $config;
    private $sessionInfos;
    public function _initialize(){
        //parent::_initialize();
        $this->sessionInfos=Session::get('yoshop_adminuser');
        // 存储配置信息
        $this->config = SettingModel::getItem('storage');
    }
    /*
     * 图片上传
     */
    public function upload($group_id=-1){
        $fileKey = request()->file('file');
        $res=Image::open($fileKey);
        $KeyId = $this->config['engine'][$this->config['default']]['access_key_id'];
        $KeySecret =$this->config['engine'][$this->config['default']]['access_key_secret'];
        $EndPoint = $this->config['engine'][$this->config['default']]['domain'];
        $Bucket = $this->config['engine'][$this->config['default']]['bucket'];
        $ossClient = new OssClient($KeyId, $KeySecret, $EndPoint);
        $fname='接口测试/测试图片/';
        $fileName = $fname. sha1(date('YmdHis', time()) . uniqid()) . '.' . $res->type();
        //执行阿里云上传
        $result = $ossClient->uploadFile($Bucket, $fileName, $fileKey->getInfo()['tmp_name']);
        $fileType =$fileKey->getInfo()['type'];
        // 图片信息
        $fileInfo = $fileKey->getInfo();
        $uploadFile = $this->addUploadFile($group_id, $fileName, $fileInfo, $fileKey->getInfo()['name'],$fileType);
        // 图片上传成功
        return json(['code' => 1, 'msg' => '图片上传成功', 'data' => $uploadFile]);
    }
    /**
     * 添加文件库上传记录
     */
    private function addUploadFile($group_id, $fileName, $fileInfo, $old_name,$fileType){
        // 存储引擎
        $storage = $this->config['default'];
        // 存储域名
        $fileUrl = isset($this->config['engine'][$storage]['domain'])?$this->config['engine'][$storage]['domain'] : '';
        // 添加文件库记录
        $model = new UploadFile;
        //记录上传图片的公司id
        $model->add([
            'group_id' => $group_id > 0 ? (int)$group_id : 0,
            'storage' => $storage,
            'file_url' => 'https://yh.globalbt.cn',
            'file_name' => $fileName,
            'file_size' => $fileInfo['size'],
            'file_type' => $fileType,
            'old_name' => $old_name,
            'companyId'=> $this->sessionInfos['user']['company_id'],
            'extension' => pathinfo($fileInfo['name'], PATHINFO_EXTENSION),
        ]);
        return $model;
    }
    /*
     * 表单提交
     */
    public function submit(){
        if(empty(Request()->post()['where'])){
            return json(['code' => -1, 'msg' => 'where条件不能为空', 'data' => []]);
        }
        if(empty(Request()->post()['name'])){
            return json(['code' => -1, 'msg' => '名称不能为空', 'data' => []]);
        }
        if(empty(Request()->post()['phone'])){
            return json(['code' => -1, 'msg' => '手机号不能为空', 'data' => []]);
        }

        return json(['code' => 1, 'msg' => '数据提交成功', 'data' => Request()->post()]);
    }
    /*
     * 详情页显示
     */
    public function info(){
        if(array_key_exists('where', Request()->post())&&!empty(Request()->post()['where'])){
            $data=Db::name('yonghui_company')->find();
            return json(['code' => 1, 'msg' => '数据获取成功', 'data' => $data]);
        }else{
            return json(['code' => -1, 'msg' => 'where条件不能为空', 'data' => []]);
        }
    }
    /*
     * 列表页面显示
     */
    public function listInfo(){
        if(array_key_exists('where', Request()->post())&&!empty(Request()->post()['where'])){
            $data=Db::name('yonghui_company')->paginate(1,false, [
                'query' => request()->request()
            ]);
            return json(['code' => 1, 'msg' => '数据获取成功', 'data' => $data]);
        }else{
            return json(['code' => -1, 'msg' => 'where条件不能为空', 'data' => []]);
        }
    }
    /*
     * 下拉数据
     */
    public function selectList(){
        $data=Db::name('yonghui_company')->select();
        return json(['code' => 1, 'msg' => '数据获取成功', 'data' => $data]);
    }
    /*
     * 下拉提交
     */
    public function selectData(){
        if(array_key_exists('selectData', Request()->post())&&!empty(Request()->post()['selectData'])){
            return json(['code' => 1, 'msg' => '提交数据成功', 'data' => Request()->post()['selectData']]);
        }else{
            return json(['code' => -1, 'msg' => '提交数据未成功', 'data' => []]);
        }
    }


}
